package com.banmao.algorithm.No101To200.No150;

import java.util.ArrayDeque;
import java.util.HashSet;

public class Solution3 {

    HashSet<String> set = new HashSet<String>() {
        {
            add("+");
            add("-");
            add("*");
            add("/");
        }
    };

    public int evalRPN(String[] tokens) {

        ArrayDeque<String> deque = new ArrayDeque<>();
        for (String token : tokens) {
            if(set.contains(token)) {
                String num1 = deque.pop();
                String num2 = deque.pop();

                int result = 0;
                switch (token) {
                    case "+" :
                        result = Integer.parseInt(num1) + Integer.parseInt(num2);
                        break;
                    case "-":
                        result = Integer.parseInt(num1) - Integer.parseInt(num2);
                        break;
                    case "*":
                        result = Integer.parseInt(num1) * Integer.parseInt(num2);
                        break;
                    case "/":
                        result = Integer.parseInt(num2) / Integer.parseInt(num1);
                        break;
                }

                deque.push(String.valueOf(result));

            } else {
                deque.push(token);
            }

        }

        return Integer.parseInt(deque.pop());

    }

}
